#include "usermanagerapi.h"

UserManagerApi::UserManagerApi(QObject *parent)
    : QObject{parent}
{
    baseUrl = Config::baseUrl;

    manager = new QNetworkAccessManager(0);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());
}

QNetworkReply *UserManagerApi::sys_normal_disable()
{
    request.setUrl(QUrl(baseUrl + "system/dict/data/type/sys_normal_disable"));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}

QNetworkReply *UserManagerApi::sys_user_sex()
{
    request.setUrl(QUrl(baseUrl + "system/dict/data/type/sys_user_sex"));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}

QNetworkReply *UserManagerApi::deptTree()
{
    request.setUrl(QUrl(baseUrl + "system/user/deptTree"));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}

QNetworkReply *UserManagerApi::initPassword()
{
    request.setUrl(QUrl(baseUrl + "system/config/configKey/sys.user.initPassword"));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}

QNetworkReply *UserManagerApi::list(int pageNum,int pageSize,int deptId,QString userName,
                                    QString phonenumber,QString status,QString beginTime,
                                    QString endTime)
{
    QString params = "system/user/list?pageNum="+ QString::number(pageNum) +
                     "&pageSize=" + QString::number(pageSize);
    if(deptId != 0){
        params += "&deptId=" + QString::number(deptId);
    }
    if(!userName.isEmpty()){
        params += "&userName=" + userName;
    }
    if(!phonenumber.isEmpty()){
        params += "&phonenumber=" + phonenumber;
    }
    if(!status.isEmpty()){
        params += "&status=" + status;
    }
    if(beginTime != "2000/1/1" && endTime != "2000/1/1"){
        params += "&params%5BbeginTime%5D=" + beginTime + "&params%5BendTime%5D=" + endTime;
    }
    request.setUrl(QUrl(baseUrl + params));
    QNetworkReply* reply = manager->get(request);

    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();

    return reply;
}

QNetworkReply *UserManagerApi::changeStatus(int userId,QString status)
{
    request.setUrl(QUrl(baseUrl + "system/user/changeStatus"));
    //创建一个Json对象,这个Json对象就是要发送的内容，展开成Json数据如上所示
    QJsonObject jsonObj;
    jsonObj.insert("userId",userId);
    jsonObj.insert("status",status);

    QJsonDocument jsonDoc = QJsonDocument(jsonObj);
    QByteArray postData = jsonDoc.toJson(QJsonDocument::Compact);
    QNetworkReply* reply = manager->post(request,postData);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}

QNetworkReply *UserManagerApi::deleteUserByIds(QString userIds)
{
    request.setUrl(QUrl(baseUrl + "system/user/" + userIds));
    QNetworkReply* reply = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}

QNetworkReply *UserManagerApi::getPostAndRole()
{
    request.setUrl(QUrl(baseUrl + "system/user/"));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}

QNetworkReply *UserManagerApi::resetPwd(QString pwd,QString userId)
{
    request.setUrl(QUrl(baseUrl + "system/user/resetPwd"));
    QJsonObject obj;
    obj.insert("password",pwd);
    obj.insert("userId",userId);
    QJsonDocument doc(obj);
    QByteArray data = doc.toJson();
    QNetworkReply* reply = manager->put(request,data);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}

QNetworkReply *UserManagerApi::authRoleInfo(QString userId)
{
    request.setUrl(QUrl(baseUrl + "system/user/authRole/"+userId));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}

QNetworkReply *UserManagerApi::authRole(QString userId,QString roleIds)
{
    request.setUrl(QUrl(baseUrl + "system/user/authRole?userId="+userId + "&roleIds=" + roleIds));
    QByteArray b;
    QNetworkReply* reply = manager->put(request,b);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    return reply;
}
